#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
from wysiwyg import Wysiwyg
from core.ui.centralpanel import CentralPage
from core.ide.themesmanager import ThemesManager
import os
import wx.lib.agw.aui as aui

class Panel(wx.Panel, CentralPage):
    def __init__(self,parent):
        CentralPage.__init__(self,"document")
        wx.Panel.__init__(self,parent)
        self._vbox=None
        self._wysiwyg=Wysiwyg(self)
        
        self._box=wx.BoxSizer(wx.VERTICAL)
        self._box.Add(self._wysiwyg,1,wx.EXPAND)
        
        self._open=False
        self.showSearch(False)
        self.SetSizer(self._box)
        

        
        
    def openFile(self,file_):
        self.data=file_
        self.setTabTitle(os.path.basename(file_))
        self._wysiwyg.setFile(file_)
        
    def save(self):
        self._wysiwyg.save()
        
    def getLang(self):
        return self._wysiwyg.getLang()
    
    def toggleSearch(self):
        self.showSearch(not self._open)
    
    def showSearch(self,display=True):
        
        if display and not self._vbox:
            self._open=True
            
            
            self.search = SearchBar(self)
            
            self._box.Add((2,2))
            self._box.Add(self.search)
            self._box.Add((2,2))
            self._box.RecalcSizes()
            self._box.Layout()
            
            #self.
            self.Refresh()
            
            self.SetSizer(self._box)
            
            
        if not display and self._vbox:
            print "remove"
            self._open=False
            self._box.RemovePos(1)
            self._box.RemovePos(1)
            self._box.RemovePos(1)
            self._box.RecalcSizes()
            self._box.Layout()
            self.search.Destroy()
            self.search=None
            
class SearchBar2(aui.AuiToolBar):
    def __init__(self,parent):
        #wx.Panel.__init__(self,parent);
        aui.AuiToolBar.__init__(self,parent, -1, wx.DefaultPosition, wx.DefaultSize,
                         agwStyle=aui.AUI_TB_PLAIN_BACKGROUND)
        
        self.search = wx.SearchCtrl(self, size=(200,-1), style=wx.TE_PROCESS_ENTER)
        self.search.ShowSearchButton(1)
        self.search.ShowCancelButton(1)
        self.search.SetMenu(None)
        
        #self.next = wx.Button(self,-1,"Next",style = wx.BU_EXACTFIT | wx.BU_LEFT | wx.NO_BORDER)
        #self.next.SetBitmap(ThemesManager().getDefaultTheme().getImage("down","icone_16"))
        #self.previus = wx.Button(self,-1,"Previous",style = wx.BU_EXACTFIT | wx.BU_LEFT | wx.NO_BORDER)
        #self.previus.SetBitmap(ThemesManager().getDefaultTheme().getImage("up","icone_16"))
        
        self.AddControl(self.search)
        self.AddSimpleTool(-1, "ind", ThemesManager().getDefaultTheme().getImage("up",'icone_16'))

class SearchBar(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent);
        
        self.search = wx.SearchCtrl(self, size=(200,-1), style=wx.TE_PROCESS_ENTER)
        self.search.ShowSearchButton(1)
        self.search.ShowCancelButton(1)
        self.search.SetMenu(None)
        
        self.next = wx.Button(self,-1,"Next",style = wx.BU_EXACTFIT | wx.BU_LEFT | wx.NO_BORDER)
        self.next.SetBitmap(ThemesManager().getDefaultTheme().getImage("down","icone_16"))
        self.previus = wx.Button(self,-1,"Previous",style = wx.BU_EXACTFIT | wx.BU_LEFT | wx.NO_BORDER)
        self.previus.SetBitmap(ThemesManager().getDefaultTheme().getImage("up","icone_16"))
        
        self._box=wx.BoxSizer(wx.HORIZONTAL)
        self._box.Add((2,2))
        self._box.Add(self.search,0,wx.EXPAND)
        self._box.Add((2,2))
        self._box.Add(self.next,0,wx.EXPAND)
        self._box.Add((2,2))
        self._box.Add(self.previus,0,wx.EXPAND)
        self.SetSizer(self._box)
        